﻿//----------------------------------------------------------------------
// <copyright file="ManagementToolsDatabase.cs" company="NTT DATA BEEN (China)">
// Copyright (c)2012 NTT DATA BEEN (China) Information Technology Co.,Ltd.
// </copyright>
// <author> lidawei </author>
// <summary>
// システム名称　　：
// モジュールＩＤ　：
// モジュール名称　：
// 機能概要　　　　：ManagementToolsDatabase类 
// 改訂履歴　　　　：
// 2012-08-01 新規 lidawei    Been0001   
// </summary>
//---------------------------------------------------------------------- 

namespace Ntt.NttData.Been.Management.SolutionA.Sync.Operator.SourceDAL
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.SqlClient;
	using BLL;
	using Common.Config;
	using Common.Data;
	using CommonLibrary.Database.SqlServer;
	using Config;
	using Model;

	public class ManagementToolsDatabase : DatabaseOperation
	{
		#region sqlstring
		private const string GETBASEMASERVERSQL = @" SELECT [ServerID],[DatabaseConnection]  FROM  [BaseMaServerInfo]";

		private const string GETOPERATORRANKSQL = 
			@" SELECT A.[OperatorID] " 
			+ " ,A.[OperatorPass] "
			+ " ,A.[OperatorName] "
			+ " ,A.[OperatorName_kana]       "
			+ " ,A.[UpDatetime] "
			+ " ,B.[JobRank] "
			+ " ,B.[CaseID] "
			+ " FROM [ServerManagement].[dbo].[BaseOperatorInfo] A "
			+ " CROSS APPLY( "
			+ " SELECT JobRank,CaseID  "
			+ " FROM PermissionOPCaseJobRank "
			+ " WHERE OperatorID = A.OperatorID  AND "
			+ "     [CaseID] in( {0} )"
			+ " ) B ";

		private const string GETOPERATORRANKIISQL = 
			@" SELECT A.[OperatorID] " 
			+ " ,A.[OperatorPass] " 
			+ " ,A.[OperatorName] " 
			+ " ,A.[OperatorName_kana]       " 
			+ " ,A.[UpDatetime] " 
			+ " ,B.[JobRank] " 
			+ " ,B.[CaseID] " 
			+ " FROM [ServerManagement].[dbo].[BaseOperatorInfo] A " 
			+ " CROSS APPLY( " 
			+ " SELECT JobRank,CaseID  " 
			+ " FROM PermissionOPCaseJobRank " 
			+ " WHERE OperatorID = A.OperatorID " 
			+ " ) B ";
		#endregion

		/// <summary>
		/// Prevents a default instance of the <see cref="ManagementToolsDatabase"/> class from being created.
		/// </summary>
		private ManagementToolsDatabase() :base( ConfigurationManager<SyncOperatorConfig>.GetInstance(ConfigType.XmlConfig).ManagementToolsDatabaseConnectionString ) {}

		/// <summary>
		/// Gets the ma server info.
		/// </summary>
		/// <returns></returns>
		public static List<EntityBaseMaServerInfo> GetMaServerInfo()
		{
			List<EntityBaseMaServerInfo> baseMaServerInfos ;
			 try
			 {
				 using (var source  = new ManagementToolsDatabase())
				 {
					 baseMaServerInfos = source.GetBaseMaServerInfo();
				  }
			 }
			 catch (Exception ex)
			 {
				 throw ;
			 } 
			return baseMaServerInfos;
		}

		/// <summary>
		/// Gets the operators ranks.
		/// </summary>
		/// <returns></returns>
		public static List<EntitySourceOperator> GetOperatorsRanks()
		{
			List<EntitySourceOperator> entityOperators ;

			try
			{
				using (var source = new ManagementToolsDatabase())
				{
					entityOperators = source.GetOperators(null);
				}

			}
			catch (Exception ex)
			{

				throw ;
			}

			return entityOperators;

		}

		/// <summary>
		/// Gets the operators ranks.
		/// </summary>
		/// <param name="caseId">The case id.</param>
		/// <returns></returns>
		[Obsolete("从数据库中直接获得operator's rank")]
		public static List<EntitySourceOperator> GetOperatorsRanks(string[] caseId)
		{
			List<EntitySourceOperator> entityOperators ;

			try
			{
				using (var source = new ManagementToolsDatabase())
				{
				  entityOperators = source.GetOperators(caseId);
				}

			}
			catch (Exception ex)
			{
				
				throw ;
			}

			return entityOperators;

		}
		
		/// <summary>
		/// Gets the base ma server info.
		/// 得到全部的BaseMaServerInfo的记录
		/// </summary>
		/// <returns></returns>
		private List<EntityBaseMaServerInfo> GetBaseMaServerInfo()
		{
			List<EntityBaseMaServerInfo> entityBaseMaServerInfos = null;
			try
			{
					if (sqlConnection != null)
					{


						using (SqlDataReader reader = new SqlHelper().ExecuteReader(sqlConnection, CommandType.Text, GETBASEMASERVERSQL))
						{

							while (reader.Read())
							{

								var item = new EntityBaseMaServerInfo
								{
									ServerId = reader.GetString(0),
									DbConnString = reader.GetString(1)
								};

								if (entityBaseMaServerInfos == null)
								{
									entityBaseMaServerInfos = new List<EntityBaseMaServerInfo>();
								}

								entityBaseMaServerInfos.Add(item);
							}

							reader.Close();
						}
					}
			}
			catch (Exception ex)
			{
				throw;
			}

			return entityBaseMaServerInfos;
		}
		
		/// <summary>
		/// Selects the specified case id.
		/// </summary>
		/// <param name="caseId">The case id.</param>
		/// <returns></returns>
		private List<EntitySourceOperator> GetOperators(string[] caseId)
		{
			List<EntitySourceOperator> operators = null;
			try
			{
				if (sqlConnection != null)
				{
					string selectSql= GETOPERATORRANKIISQL; 

					if (caseId != null && caseId.Length > 0)
					{ 
						string paramCaseId = "'"+ string.Join("','",caseId) + "'";

						selectSql = string.Format(GETOPERATORRANKSQL, paramCaseId);
					}
				  
					

				

					using (SqlDataReader reader = new SqlHelper().ExecuteReader(sqlConnection, CommandType.Text, selectSql ))
					{

						while (reader.Read())
						{

							var item = new EntitySourceOperator
							{
								OperatorId = reader.GetStringValue("OperatorID"),
								OperatorPass = reader.GetStringValue("OperatorPass"),
								OperatorName = reader.GetStringValue("OperatorName"),
								OperatorNamekana = reader.GetStringValue("OperatorName_kana"),
								UpadteTime = reader.GetDateTimeValue("UpDatetime"),
								Rank = (JobRank)reader.GetInt32Value("JobRank"),
								CaseId = reader.GetStringValue("CaseID"),
							};

							if (operators == null)
							{
								operators = new List<EntitySourceOperator>();
							}

							operators.Add(item);
						}

						reader.Close();
					}
				}
			}
			catch (Exception ex)
			{
				throw ;
			}

			return operators;
		}
	}
}
